// pages/receiptDetail/receiptDetail.js
let request = require('../../operation/operation.js')
let userModel = require('../../models/user.js')
let noticeModel = require('../../models/notice.js')
let bxt = require('../../utils/bxt.js')
let util = require('../../utils/util.js')
let receiptModel = require('../../models/receipt.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    notice:{},
    receipt:null,
    images: [],  // 界面中显示的图片数据
    canReceipt:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getApp().notificationCenter.register(bxt.UPDATE_RECEIPT_SUCCESSED_MESSAGE, this, "refreshReceiptView")
    this.getReceiptDetail()
  },

  onUnload: function () {
    getApp().notificationCenter.remove(bxt.UPDATE_RECEIPT_SUCCESSED_MESSAGE, this)
  },

  /**
   * 点击重签按钮事件处理函数
   */
  onShowEditReceiptView:function() {
    let that = this

    if (noticeModel.RECEIPT_REQUIREMENT[1].value == this.data.notice.receiptRequirement) { // 文字签收模式
      this.enterEditReceiptView()
    }
    else if (noticeModel.RECEIPT_REQUIREMENT[2].value == this.data.notice.receiptRequirement) { // 图片模式
      wx.chooseImage({
        count: 9,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
          getApp().globalData.param.images = res.tempFilePaths
          that.enterEditReceiptView()
        }
      })
    } else if (noticeModel.RECEIPT_REQUIREMENT[3].value == this.data.notice.receiptRequirement) { // 视频模式
      wx.chooseVideo({
        sourceType: ['album', 'camera'],
        compressed: true,
        maxDuration: 60,
        success: function (res) {          
          getApp().globalData.param.video = res.tempFilePath
          that.enterEditReceiptView()
        },
        fail:function(res) {
          console.log('视频出错')
          console.log(res)
        }
      })
    }
  },

  getReceiptDetail:function() {
    var that = this;

    request.getRequest('/receipts/' + getApp().globalData.param.receipt.sid, {}, true)
      .then(res => {
        var data = res.data;
        if (request.SUCCESSED == data.status) {
          var objectReceipt = data.object
          that.refreshReceiptView(objectReceipt)
          that.initReceiptButton(objectReceipt)
        } else {
          wx.showToast({
            title: data.msg,
            iocn: 'none'
          })
        }
      })
      .catch(e => {
        console.log(e)
      })
  },

  /**
  * 点击图片预览事件处理函数
  */
  previewImage: function (event) {
    var that = this
    wx.previewImage({
      current: event.currentTarget.dataset.path.url,
      urls: receiptModel.getImagesUrl(that.data.images)
    })
  },

  /**
  * 点击播放视频事件处理函数
  */
  onPlayVideo: function (e) {
    wx.navigateTo({
      url: '../playVideo/playVideo?src=' + this.data.receipt.video.url,
    })
  },

  /**
   * 显示重签按钮
   * 自己的签收，同时通知签收模式非“无”可以进行重新签收操作
   */
  initReceiptButton:function(receipt) {
    let currentUser = userModel.getCurrentUser()

    if (currentUser.sid == receipt.creatorSid && getApp().globalData.param.notice.receiptRequirement != noticeModel.RECEIPT_REQUIREMENT[0].value) {
      this.setData({
        canReceipt:true,
        receiptButtonDesc: noticeModel.getReceiptRequirement(getApp().globalData.param.notice.receiptRequirement).name
      })
    }
  },

  enterEditReceiptView:function() {
    wx.navigateTo({
      url: '../receiptNotice/receiptNotice?receiptSid=' + getApp().globalData.param.receipt.sid,
    })
  },

  refreshReceiptView:function(receipt) {
    if (receipt.receiptObject) {  // 重签成功后，返回的数据是这样的格式
      receipt = receipt.receiptObject
    }

    var tmpImages = []
    if (receipt.images) {
      tmpImages = receipt.images
    }
    if (null == receipt.createdAt) {  // 重签后，数据没有回时间，这里需要使用之前的时间
      receipt.createdAt = this.data.receipt.createdAt
    }
    receipt.uiCreatedAt = util.formatDateTime(receipt.createdAt)
    this.setData({
      notice: getApp().globalData.param.notice,
      receipt: receipt,
      images: tmpImages
    })
  }
})